package it.lapomuge.gui;

import it.lapomuge.db.dao.DbManagerGare;
import it.lapomuge.db.entity.Giudice;
import it.lapomuge.gui.model.ModelGiudici;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;

public class JDialogImportaGiudici extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private JTextField textField;

	

	/**
	 * Create the dialog.
	 */
	public JDialogImportaGiudici() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		{
			GridBagLayout gbl_contentPanel = new GridBagLayout();
			gbl_contentPanel.columnWidths = new int[]{156, 0, 1, 119, 0, 0};
			gbl_contentPanel.rowHeights = new int[]{25, 0};
			gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
			gbl_contentPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
			contentPanel.setLayout(gbl_contentPanel);
			JButton btnSelezionaFile = new JButton("Seleziona File..");
			btnSelezionaFile.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					openFile();
				}
			});
			{
				JLabel lblSeparatore = new JLabel("Separatore");
				GridBagConstraints gbc_lblSeparatore = new GridBagConstraints();
				gbc_lblSeparatore.anchor = GridBagConstraints.EAST;
				gbc_lblSeparatore.insets = new Insets(0, 0, 0, 5);
				gbc_lblSeparatore.gridx = 0;
				gbc_lblSeparatore.gridy = 0;
				contentPanel.add(lblSeparatore, gbc_lblSeparatore);
			}
			{
				textField = new JTextField();
				textField.setText(",");
				GridBagConstraints gbc_textField = new GridBagConstraints();
				gbc_textField.insets = new Insets(0, 0, 0, 5);
				gbc_textField.fill = GridBagConstraints.HORIZONTAL;
				gbc_textField.gridx = 1;
				gbc_textField.gridy = 0;
				contentPanel.add(textField, gbc_textField);
				textField.setColumns(10);
			}
			GridBagConstraints gbc_btnSelezionaFile = new GridBagConstraints();
			gbc_btnSelezionaFile.insets = new Insets(0, 0, 0, 5);
			gbc_btnSelezionaFile.anchor = GridBagConstraints.NORTHWEST;
			gbc_btnSelezionaFile.gridx = 3;
			gbc_btnSelezionaFile.gridy = 0;
			contentPanel.add(btnSelezionaFile, gbc_btnSelezionaFile);
		}
		{
			JButton btnSalva = new JButton("Salva...");
			btnSalva.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					salva();
				}
			});
			GridBagConstraints gbc_btnSalva = new GridBagConstraints();
			gbc_btnSalva.gridx = 4;
			gbc_btnSalva.gridy = 0;
			contentPanel.add(btnSalva, gbc_btnSalva);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			table = new JTable();
			getContentPane().add(table, BorderLayout.CENTER);
			table.setModel(new ModelGiudici());
		}
	}

	protected void salva() {
		ModelGiudici model = (ModelGiudici) table.getModel();
		List<Giudice> list = model.getList();
		if(list != null){			
			DbManagerGare.getInstance().salvaGiudici(list);
		}
		
		model.fireTableDataChanged();
		
	}

	protected void openFile() {
		JFileChooser f = new JFileChooser();
		FileFilter ff = new FileFilter() {
			
			@Override
			public String getDescription() {
				
				return "CVS (.cvs)";
			}
			
			@Override
			public boolean accept(File f) {
				return f.getAbsolutePath().endsWith(".csv");
			}
		};
		f.setFileFilter(ff);
		
		if(f.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
			List<Giudice> atleti = new ArrayList<Giudice>();
			File file = f.getSelectedFile();
			String line = null;
			try {
				BufferedReader br = new BufferedReader(new FileReader(file));
				
				line = br.readLine();
				while(line != null){
					String[] l = line.split(textField.getText());
					
					Giudice a = new Giudice();
					a.setCognome(l[0]);
					a.setNome(l[1]);
					a.setLogin(l[2]);
					a.setPassword(l[3]);
					atleti.add(a);			
					
					line = br.readLine();						
				}
				
				ModelGiudici model = (ModelGiudici) table.getModel();
				model.setList(atleti);
				model.fireTableDataChanged();
				
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "Errore nell'importazione: "+ (line == null ? e.getMessage() : line));
			}
			
			
		}
		
		
		
	}

}
